﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WinFormClient.ServiceReference1;

namespace WinFormClient
{
    public partial class LoginForm : Form
    {
        Client client;
        public LoginForm()
        {
            InitializeComponent();
            client = new Client();
            client.debug = 100;
            client.FinishedLoadUserInfo += new EventHandler(client_FinishedLoadUserInfo);
        }

        void client_FinishedLoadUserInfo(object sender, EventArgs e)
        {
            UserInfo user = sender as UserInfo;
            TxtLogUserId.Text = user.UserName;
            TxtLogPassword.Text = user.Password;
        }

        private void BtnSignUpChange_Click(object sender, EventArgs e)
        {
            LabPasswordMakeSure.Visible = true;
            TxtPasswordMakeSure.Visible = true;
            BtnLogin.Text = "注册";
        }

        private void BtnLogin_Click(object sender, EventArgs e)
        {
            if (TxtLogUserId.Text == "" || TxtLogPassword.Text == "")
            {
                MessageBox.Show("用户名密码不能为空！");
                return;
            }
            if (LabPasswordMakeSure.Visible == true)//注册状态
            {
                if (TxtLogPassword.Text != TxtPasswordMakeSure.Text)
                {
                    TxtLogPassword.Clear();
                    TxtPasswordMakeSure.Clear();
                    MessageBox.Show("两次输入密码不一致");
                }              
                else
                {
                    AssignState Whethersuc =  client.SignUp(TxtLogUserId.Text, TxtLogPassword.Text);
                    if(Whethersuc==AssignState.SUCCEED) 
                    {
                        MessageBox.Show("注册成功!请返回登录");
                        LabPasswordMakeSure.Visible = false;
                        TxtPasswordMakeSure.Visible = false;
                        BtnLogin.Text = "登录";
                    }
                    else
                    {
                        switch (Whethersuc)
                        {
                            case AssignState.NAME_USED: MessageBox.Show("账号已被使用！"); break;
                            case AssignState.NAME_EMPTY: MessageBox.Show("账号为空！"); break;
                            case AssignState.NAME_ERROR_CHAR: MessageBox.Show("账号中使用了非法字符！"); break;
                            case AssignState.NAME_TOO_LONG: MessageBox.Show("账号过长！"); break;
                            case AssignState.PWD_ERROR_CHAR: MessageBox.Show("密码中使用了非法字符！"); break;
                            case AssignState.PWD_TOO_LONG: MessageBox.Show("密码过长！"); break;
                            case AssignState.ERROR_WHILE_READ_DB: MessageBox.Show("数据库错误囧，不好意思，请重新再来"); break;
                            default: MessageBox.Show("恭喜您发现了一个连开发者都没想到的错误！请联系186XXXX2746!"); break;
                        }                    
                    }                    
                }
            }
            else//登录状态
            {
                LoginState WhetherLogin;
                WhetherLogin=client.Login(TxtLogUserId.Text, TxtLogPassword.Text);
                if (WhetherLogin == LoginState.LOGIN)
                {
                    this.Hide();
                    MainForm MForm = new MainForm(client);
                    MForm.Show();
                }
                else
                {
                    switch (WhetherLogin)
                    {
                        case LoginState.USERNOTEXIST: MessageBox.Show("账号不存在！"); break;
                        case LoginState.WRONGPASSWORD: MessageBox.Show("密码错误！"); break;
                        case LoginState.DBERROR: MessageBox.Show("数据库错误！"); break;
                        default: MessageBox.Show("恭喜您发现了一个连开发者都没想到的错误！请联系186XXXX2746!"); break;
                    }
                }
            }
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            client.FetchUserInfo();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MainForm form = new MainForm(client);
            form.Show();
        }
    }
}
